package com.lbh.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.CronCalendar;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.lbh.job.ThreeJob;
import com.lbh.model.JobModel;

public class ThreeController extends Controller {

	public void index() {
		List<JobModel> job = JobModel.JOB.getJob();
		this.setAttr("job", job);
		render("cron/three.html");
	}

	public void cron() {
		render("cron/cron.html");
	}

	/**
	 * 生成cron最近五次运行时间
	 */
	public void getCron() {
		String cron = this.getPara("cron");
		List<String> arr = Lists.newArrayList();
		try {
			CronCalendar cronCalendar = new CronCalendar(cron);
			Date date = new Date();
			for (int i = 0; i < 5; i++) {
				Date nextValidTimeAfter = cronCalendar.getCronExpression().getNextValidTimeAfter(date);
				if (nextValidTimeAfter != null) {
					arr.add(nextValidTimeAfter.toLocaleString());
					date = nextValidTimeAfter;
				} else {
					arr.add("暂无数据");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderJson(arr);
	}

	/**
	 * 添加定时方法
	 */
	public void addScheduler() {
		Map<String, Object> map = Maps.newHashMap();
		String cron = this.getPara("cron");
		String content = this.getPara("content");
		if (StringUtils.isBlank(cron) || StringUtils.isBlank(content)) {
			map.put("msg", "参数不能为空");
			map.put("code", "14000");
		} else {
			JobModel job = new JobModel();
			job.set(job.CRON, cron);
			job.set(job.CONTENT, content);
			job.set(job.TIME, new Date());
			job.set(job.TYPE, 2);
			boolean save = job.save();
			if (save) {
				map.put("msg", "发布成功");
				map.put("code", "13000");
			}
		}
		renderJson(map);
	}

	/**
	 * 开启调度器
	 */
	@Before(Tx.class)
	public void schedulerStart() {
		String id = this.getPara("id");
		Random rd = new Random(4);
		Random rds = new Random(3);
		JobModel job = JobModel.JOB.getJob(id);
		Map<String, Object> map = Maps.newHashMap();
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();
			JobDetail jobs = JobBuilder.newJob(ThreeJob.class).withIdentity("job" + rd + rds, "group" + rd + rds)
					.build();
			CronTrigger trigger = TriggerBuilder.newTrigger().startNow()
					.withIdentity("trigger" + rd + rds, "group" + rd + rds)
					.withSchedule(CronScheduleBuilder.cronSchedule(job.getCron())).build();
			Date scheduleJob = scheduler.scheduleJob(jobs, trigger);
			if (scheduleJob != null) {
				job.setJobName("job" + rd + rds);
				job.setTriggerName("group" + rd + rds);
				job.setGroupName("group" + rd + rds);
				job.setType("1");
				boolean update = job.update();
				if (update) {
					map.put("msg", "开启成功");
					map.put("code", 13000);
				} else {
					map.put("msg", "开启失败");
					map.put("code", 14000);
				}
			} else {
				map.put("msg", "开启失败");
				map.put("code", 14000);
			}
			renderJson(map);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/*
	 * /* 删除
	 */
	public void schedulerEnd() {
		String id = this.getPara("id");
		JobModel job = JobModel.JOB.getJob(id);
		Scheduler scheduler;
		Map<String, Object> map = Maps.newHashMap();
		map.put("msg", "删除失败");
		map.put("code", 14000);
		if (java.util.Objects.isNull(job)) {
			renderJson(map);
			return;
		}
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			boolean deleteJob = scheduler.deleteJob(new JobKey(job.getJobName(), job.getGroupName()));
			if (deleteJob) {
				job.setType("2");
				job.update();
				map.put("msg", "删除成功");
				map.put("code", 13000);
			}
			renderJson(map);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
